<template>
  <div>
    <div class="photo-no" v-if="total==0">
      <!--noData-->
      <div class="no-img"><img src="../../../../../static/images/collection/no-img.png" alt=""></div>
      <p class="photo-no-text">暂无藏品照片</p>
      <div class="photo-no-btn">
        <router-link :to="{name:'uploadPhoto'}">立即拍照</router-link>
      </div>
    </div>

    <div class="photo-body" v-else>
      <div class="photo-header-search" v-if="!isShowRemove">
        <div class="weui-search-bar__box">
          <input type="search" ref="searchInput" class="weui-search-bar__input" :class="{focus:isFocus}" placeholder="请输入照片名称" autocomplete="off" v-model="photoName" @keyup="searchEnter($event)" @blur="blurSearch" @focus="getFocus"/>
          <span class="clear-search" v-show="isFocus" @click="clearSearch"></span>
        </div>

        <div class="photo-header-select">
          <img src="../../../../../static/images/collection/sanjiao.png" alt="">
          <div class="photo-dynasty" @click="dropDown">{{dynastyName}}</div>
          <ul class="photo-dynasty-dropdown" v-show="isDropDown">
            <li @click="selectDynasty('all')">全部朝代</li>
            <li v-for="(v,k) in dynastyList" :key="k" @click="selectDynasty(v)">{{v.name}} ({{v.count}})</li>
          </ul>
        </div>

        <div class="photo-remove" @click="clickRemoveImg"></div>
      </div>

      <header class="select-photo-header" v-else>
        <div class="select-header-left" @click="cancel">取消</div>
        <p class="select-header-center">{{removeImgTotal}}张照片、{{removeVideoTotal}}个视频</p>
        <div class="select-header-right" @click="clickRemoveText">删除</div>
      </header>

      <div class="search-no" v-if="total!=0&&this.collectionList.length==0">
        <!--noData-->
        <div class="no-img"><img src="../../../../../static/images/collection/no-img.png" alt=""></div>
        <p class="photo-no-text">暂无藏品照片</p>
        <p class="search-no-explain">可以在照片详情中编辑名称及朝代方便您搜索查询</p>
      </div>

      <div class="photo-list-main" v-else>
        <div class="photo-list-box" v-for="(item,index) in even(collectionList)" :key="index">
          <p class="photo-list-time">{{item.time}} {{item.time | week}}</p>
          <ul class="photo-list-img-box">
            <li class="photo-list-img" v-for="(v,k) in item.photoList" :key="k" @click="goPhotoDetail(v.id)">
              <div class="div-img" v-if="v.smImageUrl" :style="'backgroundImage: url('+(v.smImageUrl.substr(0,4).toLowerCase() == 'http' ? v.smImageUrl : resUrl+v.smImageUrl)+')'"></div>
              <span class="phpto-num" v-if="v.duration == 0">{{v.imgNum+v.videoNum}}张</span>
              <p class="phpto-video" v-if="v.duration != 0">{{v.duration | timeStamp}}</p>
              <label class="photo-check-label" :for="v.id" v-show="isShowRemove">
                <div>
                  <input type="checkbox" name="checkbox" class="weui-check" :id="v.id" :value="v" v-model="removeIdList">
                  <i class="weui-icon-checked"></i>
                </div>
              </label>
            </li>
          </ul>
        </div>
        <p class="photo-list-num">{{imgTotal}}张照片，{{videoTotal}}个视频</p>
      </div>
      <router-link :to="{name:'uploadPhoto'}" class="camera"></router-link>
    </div>

  </div>
</template>

<script>
  import Moment from 'moment';
  import {ipconfig} from '../../../../assets/js/ipconfig';
  import {timeMsec} from "../../../../assets/js/tools";
  import {getAlbumDynastyList,getCollectionList,deleteCollection} from '../../../../assets/js/getData';
  export default {
    name: "photo",
    data(){
      return {
        resUrl:ipconfig.resUrl,//图片、视频、音频等资源访问路径
        firstNum:null,//第一次加载数据
        dynastyList:[],//朝代列表
        isDropDown:false,//是否显示朝代下拉框
        dynastyId:null,//当前选择朝代ID
        dynastyName:'全部朝代',//当前选择朝代名称
        photoName:'',//照片名称
        collectionList:[],//藏品列表
        isFocus:false,
        isShowRemove:false,//是否显示删除
        imgTotal:0,//图片总数
        videoTotal:0,//视频总数
        removeIdList:[],//要删除的藏品ID列表{id:v.id}
        total:0,//藏品总数
      }
    },
    created(){
      //获取朝代列表
      this.getAlbumDynastyList();
      //获取藏品列表
      this.getCollectionList();
      //获取藏品总数
      this.getTotal();

      Moment.locale('zh-cn',{
        weekdays : [
          "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"
        ]
      });
    },
    filters: {
      week: (value) => {
        if(!value){
          return value;
        }
        let time = timeMsec(value);
        return Moment(time).format('dddd');
      }
    },
    computed:{
      removeImgTotal(){
        let removeImgTotal = 0;
        for(let v of this.removeIdList){
          removeImgTotal += v.imgNum;
        }
        return removeImgTotal;
      },
      removeVideoTotal(){
        let removeVideoTotal = 0;
        for(let v of this.removeIdList){
          removeVideoTotal += v.videoNum;
        }
        return removeVideoTotal;
      }
    },
    methods:{
      //处理藏品列表函数
      even(arr) {
        let list = [];
        for (let i=0;i<arr.length;i++){
          arr[i].createTime = arr[i].createTime.slice(0,10);
          if(i==0){
            list.push({time:arr[i].createTime,photoList:[]});
          }else if(arr[i].createTime != arr[i-1].createTime){
            list.push({time:arr[i].createTime,photoList:[]});
          }
        }
        for(let i=0;i<arr.length;i++){
          for(let j=0;j<list.length;j++){
            if(arr[i].createTime==list[j].time){
              list[j].photoList.push(arr[i]);
            }
          }
        }
        return list;
      },
      //点击删除藏品
      clickRemoveText(){
        let ids = [];
        for(let v of this.removeIdList){
          ids.push({id:v.id});
        }
        if(ids.length==0){
          Toast('请选择要删除的藏品');
          return;
        }
        MessageBox.confirm('确定删除吗？删除后，无法恢复','').then(() => {
          deleteCollection({ids:ids}).then(res => {
            if(res.data.reqResult.code > 0){
              this.isShowRemove = false;
              this.getCollectionList();
              this.getAlbumDynastyList();
              this.getTotal();
            }else {
              Toast(res.data.reqResult.msg);
            }
          })
        }).catch(()=>{})
      },
      //点击取消
      cancel(){
        this.isShowRemove = false;
      },
      //点击删除图标
      clickRemoveImg(){
        this.isShowRemove = true;
      },
      //点击图片进入图片详情
      goPhotoDetail(id){
        if(this.isShowRemove){
          return;
        }
        this.$router.push({name:'photoDetail',query:{photoId:id}});
      },
      //显示朝代下拉框
      dropDown(){
        this.isDropDown = !this.isDropDown;
      },
      //选择朝代
      selectDynasty(v){
        if(v=='all'){
          this.dynastyId = null;
          this.dynastyName = '全部朝代';
        }else {
          this.dynastyId = v.dynastyId;
          this.dynastyName = v.name;
        }
        this.isDropDown = !this.isDropDown;
        this.getCollectionList();
      },
      //搜索
      searchEnter(e){
        if(e.keyCode == 13&&this.photoName){
          this.$refs.searchInput.blur();
          this.getCollectionList();
        }
      },
      //失去焦点
      blurSearch(){
        if(!this.photoName){
          this.isFocus = false;
          // return;
        }
        this.getCollectionList();
      },
      //获取焦点
      getFocus(){
        this.isFocus = true;
      },
      //清空搜索
      clearSearch(){
        this.isFocus = false;
        this.photoName = '';
        this.getCollectionList();
      },
      //获取藏品列表
      async getCollectionList(){
        let params = {
          name:this.photoName,
          dynastyId:this.dynastyId,
          // pageNum:1,
          // pageSize:10,
        };
        let res = await getCollectionList(params);
        if(res.data.reqResult.code > 0){
          this.collectionList = res.data.data;
          console.log(res);
          let imgTotal = 0;
          let videoTotal = 0;
          for(let v of this.collectionList){
            imgTotal += v.imgNum;
            videoTotal += v.videoNum;
          }
          this.imgTotal = imgTotal;
          this.videoTotal = videoTotal;
        }else {
          Toast(res.data.reqResult.msg);
        }
      },
      //获取藏品总数
      getTotal(){
        getCollectionList().then(res => {
          if(res.data.reqResult.code > 0){
            this.total = res.data.data.length;
          }else {
            Toast(res.data.reqResult.msg);
          }
        });
      },
      //获取朝代列表
      getAlbumDynastyList(){
        getAlbumDynastyList().then(res => {
          if(res.data.reqResult.code > 0){
            this.dynastyList = res.data.data;
          }else {
            Toast(res.data.reqResult.msg);
          }
        });
      }
    }
  }
</script>

<style scoped>
  .photo-no{
    margin-top: 8.45rem;
  }
  .no-img{
    width: 4.4rem;
    margin: 0 auto;
  }
  .no-img img{
    width: 100%;
  }
  .photo-no-text{
    color: #666666;
    font-size: 0.7rem;
    text-align: center;
    line-height: 0.7rem;
    margin-top: 0.75rem;
    margin-bottom: 0.85rem;
  }
  .photo-no-btn{
    text-align: center;
  }
  .photo-no-btn a{
    display: inline-block;
    width: 4.9rem;
    height: 1.35rem;
    line-height: 1.35rem;
    color: #ffffff;
    font-size: 0.7rem;
    background-color: #C50000;
    border-radius: 13px;
  }

  .photo-body{
    padding-top: 3rem;
    padding-bottom: 3rem;
  }
  .photo-header-search{
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 0.75rem 0.85rem 0.75rem;
    border-bottom: 1px solid #EEEEEE;
    box-sizing: border-box;
    background-color: #ffffff;
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    z-index: 10;
  }
  .weui-search-bar__box{
    width: 57.4%;
    height: auto;
    padding: 0;
  }
  .weui-search-bar__box .weui-search-bar__input{
    width: 100%;
    height: 1.35rem;
    border: 1px solid #999999;
    border-radius: 14px;
    line-height: 1.25rem;
    color: #666666;
    font-size: 0.7rem;
    padding: 0;
    padding-left: 1.55rem;
    box-sizing: border-box;
    background-image: url("../../../../../static/images/home/search.png");
    background-repeat: no-repeat;
    background-size: 0.6rem;
    background-position: 0.6rem center;
  }
  .weui-search-bar__box .weui-search-bar__input.focus{
    background: none;
    padding-left: 0.85rem;
    padding-right: 1.5rem;
  }
  .clear-search{
    width: 0.85rem;
    height: 0.85rem;
    background-image: url("../../../../../static/images/collection/clear.png");
    background-repeat: no-repeat;
    background-size: contain;
    position: absolute;
    right: 0.65rem;
    top: 50%;
    transform: translateY(-50%);
  }
  /*表单placeholder样式*/
  input::-webkit-input-placeholder {
    color: #999999;
    font-size: 0.7rem;
  }
  input:-moz-placeholder {
    color: #999999;
    font-size: 0.7rem;
  }
  input::-moz-placeholder {
    color: #999999;
    font-size: 0.7rem;
  }
  input:-ms-input-placeholder {
    color: #999999;
    font-size: 0.7rem;
  }
  .photo-header-select{
    width: 29%;
    height: 1.35rem;
    border: 1px solid #999999;
    border-radius: 14px;
    position: relative;
  }
  .photo-header-select img{
    width: 0.45rem;
    position: absolute;
    top: 0.55rem;
    right: 0.5rem;
  }
  .photo-dynasty{
    height: 100%;
    line-height: 1.25rem;
    padding-left: 0.55rem;
    color: #333333;
    font-size: 0.7rem;
  }
  .photo-dynasty-dropdown{
    width: 4.65rem;
    background-color: #fff;
    border-radius: 4px;
    box-shadow: 0 0 5px 0 rgba(0,0,0,0.3);
    position: absolute;
    top: 1.6rem;
    left: -1px;
    padding: 0.25rem 0;
    max-height: 20rem;
    overflow: auto;
  }
  .photo-dynasty-dropdown li{
    height: 1.3rem;
    line-height: 1.3rem;
    border-bottom: 1px solid #EEEEEE;
    text-align: center;
    font-size: 0.6rem;
  }
  .photo-dynasty-dropdown li:last-child{
    border-bottom:none;
  }
  .photo-remove{
    width: 0.9rem;
    height: 1.05rem;
    background: url("../../../../../static/images/collection/delete_1.png") no-repeat;
    background-size: contain;
  }

  .search-no{
    margin-top: 4.9rem;
  }
  .search-no-explain{
    color: #333333;
    font-size: 0.7rem;
    width: 10.25rem;
    line-height: 1.2rem;
    text-align: center;
    margin: 0 auto;
  }

  .photo-list-main{
    /*padding-top: 0.5rem;*/
  }
  .photo-list-box{
    /*padding: 0 0.75rem;*/
  }
  .photo-list-time{
    height: 2.2rem;
    line-height: 2.2rem;
    color: #282828;
    font-size: 0.7rem;
    padding: 0 4%;
  }
  .photo-list-img-box{
    display: flex;
    flex-wrap: wrap;
    padding: 0 4%;
  }
  .photo-list-img{
    width: 29.9vw;
    height: 29.9vw;
    margin-right: 1.15vw;
    margin-bottom: 1.15vw;
    position: relative;
    background-color: rgba(0,0,0,0.2);
  }
  .div-img{
    width: 100%;
    height: 100%;
    background-origin: content-box;
    background-position:50% 50%;
    background-size:contain;
    background-repeat:no-repeat;
    background-color: rgba(0,0,0,0.2);
  }
  .photo-list-img-box .photo-list-img:nth-child(3n+3){
    margin-right: 0;
  }
  .photo-list-img img{
    width: 100%;
  }
  .phpto-num{
    display: inline-block;
    background-color: rgba(0,0,0,0.65);
    color: #ffffff;
    font-size: 0.5rem;
    padding: 0.05rem 0.4rem;
    position: absolute;
    bottom: 0.5rem;
    left: 0.5rem;
    line-height: 0.5rem;
  }
  .phpto-video{
    position: absolute;
    left: 0.65rem;
    bottom: 0.5rem;
    color: #F4F4F4;
    font-size: 0.7rem;
    height: 0.7rem;
    line-height: 0.7rem;
    padding-left: 1.45rem;
    background-image: url("../../../../../static/images/collection/video.png");
    background-repeat: no-repeat;
    background-size: 0.85rem;
    background-position: left center;
  }
  .camera{
    display: block;
    width: 3rem;
    height: 3rem;
    background-image: url("../../../../../static/images/collection/camera.png");
    background-repeat: no-repeat;
    background-size: contain;
    position: fixed;
    right: 0.8rem;
    bottom: 3.25rem;
  }
  .photo-check-label{
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
  }
  .photo-check-label>div{
    width: 0.9rem;
    height: 0.9rem;
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
  }
  .weui-icon-checked:before {
    content: '\EA01';
    color: #ffffff;
    font-size: 0.9rem;
    display: block;
    margin: 0;
  }
  .weui-check:checked + .weui-icon-checked:before {
    content: '\EA06';
    color: #C50000;
  }
  .select-photo-header{
    display: flex;
    justify-content: space-between;
    height: 2.9rem;
    line-height: 2.9rem;
    border-bottom: 1px solid #EEEEEE;
    background-color: #ffffff;
    padding: 0 0.75rem;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 10;
  }
  .select-header-left,.select-header-center{
    color: #000000;
    font-size: 0.7rem;
  }
  .select-header-right{
    color: #666666;
    font-size: 0.7rem;
  }
  .photo-list-num{
    color: #333333;
    font-size: 0.7rem;
    margin-top: 0.85rem;
    text-align: center;
  }
</style>
